package com.changgou.order.service.impl;

import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LQQ
 * @Date 2020/3/11 14:56
 * @Version 1.0
 */

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SkuFeign skuFeign;
    private static final String CART = "cart_";//定义一个常量

    /**
     * 添加购物车
     *
     * @param skuId
     * @param num
     * @param username
     */
    public void addCart(String skuId, Integer num, String username) {
        //购物车在Redis中以hash形式存储
        //1. 查询Redis中相对应的商品信息
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);

        //2. 如果当前商品在Redis中存在,则更新商品的数量与价钱
        if (orderItem != null) {
            //更新数量
            orderItem.setNum(orderItem.getNum() + num);

            if (orderItem.getNum() <= 0) {
                //删除该商品
                redisTemplate.boundHashOps(CART + username).delete(skuId);
                return;
            }

            //更新价钱
            orderItem.setMoney(orderItem.getNum() * orderItem.getPrice());
            orderItem.setPayMoney(orderItem.getNum() * orderItem.getPrice());
        } else {
            //3. 商品在Redis中不存在,将商品添加到Redis中
            Sku sku = skuFeign.findById(skuId).getData();

            Spu spu = skuFeign.findSpuById(sku.getSpuId()).getData();

            //封装
            orderItem = this.sku2OrderItem(sku, spu, num);
        }


        //4. 将orderItem添加到Redis中
        redisTemplate.boundHashOps(CART + username).put(skuId, orderItem);

    }

    /**
     * 查询购物车列表数据
     *
     * @param username
     * @return
     */

    public Map list(String username) {

        Map map = new HashMap();
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();

        map.put("orderItemList", orderItemList);

        //还要添加商品的总数量和总价格
        Integer totalNum = 0;
        Integer totalMoney = 0;

        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();//总数量
            totalMoney += orderItem.getMoney();//总价钱
        }


        map.put("totalNum", totalNum);
        map.put("totalMoney", totalMoney);


        return map;


    }

    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {

        OrderItem orderItem = new OrderItem();

        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(orderItem.getPrice() * num);//不懂
        orderItem.setPayMoney(orderItem.getPrice() * num);
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight() * num);
        //分类信息
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());


        return orderItem;
    }
}
